package de.andreavicentini.teatralia.pages;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.magiclabs.htmlx.Environment;
import org.magiclabs.htmlx.ProgrammaticTextStructure;
import org.magiclabs.htmlx.TextStructure;


public class TDate {

	private final Date date;
	private final Types type;

	private final static TextStructure TOKENS = new ProgrammaticTextStructure() {{
		this.add(Types.complete)
			.add(Locale.GERMAN, "dd MMMMM yyyy")
			.add(Locale.ITALIAN, "dd MMMMM yyyy")
			;
		this.add(Types.date_and_time)
		.add(Locale.GERMAN, "dd MMMMM yyyy 'um' HH.mm 'Uhr'")
		.add(Locale.ITALIAN, "dd MMMMM yyyy 'ore' HH.mm")
		;
		this.add(Types.onlyyear)
		.add(Locale.GERMAN, "yyyy")
		.add(Locale.ITALIAN, "yyyy")
		;
	}};
	
	private enum Types implements TextStructure.TextKey {
		complete, onlyyear, date_and_time,
	}
	
	public TDate(int year, int month, int day) {
		this.type = Types.complete;
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, day);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.YEAR, year);
		this.date = calendar.getTime();
	}
	public TDate(int year, int month, int day, int hour, int minute) {
		this.type = Types.date_and_time;
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, day);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		this.date = calendar.getTime();
	}
	
	public TDate(int year) {
		this.type = Types.onlyyear;
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		this.date = calendar.getTime();
	}
	
	@Override
	public String toString() {
		return new SimpleDateFormat(TOKENS.get(this.type).original(), Environment.instance.getLocale()).format(date);
	}
	
	public long getTime() { return this.date.getTime(); }
}
